<html>
<head><meta charset="utf-8"><title>Did we stabilized the layout of &amp;[T], &amp;dyn Trait? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html">Did we stabilized the layout of &amp;[T], &amp;dyn Trait?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="236733215"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/236733215" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#236733215">(Apr 29 2021 at 19:42)</a>:</h4>
<p>I searched the RFC repo but didn't find anything which implies that the layout of DST-pointers like &amp;[T] or &amp;dyn Trait had been stabilized, on the other hand the official unsafe code guid lines imply that exactly that is the case. (<a href="https://rust-lang.github.io/unsafe-code-guidelines/layout/pointers.html">https://rust-lang.github.io/unsafe-code-guidelines/layout/pointers.html</a>)</p>
<p>I personal don't think we ever will change it for x86,ARM and I also think it's unlikely that there is an arch where a different layout is preferable, but as long was we didn't stabilize it officially it would be bad to advertise it at such. (Or I'm just blind and missed it when searching RFC's and the internals user forum <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span> ).</p>



<a name="236737745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/236737745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#236737745">(Apr 29 2021 at 20:13)</a>:</h4>
<p>unsafe code guidelines are not official yet, and this is one of the main cases in the book where it makes a definitive statement that is not yet stable elsewhere</p>



<a name="236739619"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/236739619" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#236739619">(Apr 29 2021 at 20:27)</a>:</h4>
<p>Ok, thanks a lot for the fast feedback.</p>



<a name="236754955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/236754955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#236754955">(Apr 29 2021 at 22:08)</a>:</h4>
<p>Is it reasonable to treat the unsafe guide lines as: "people which are deeply involved in the whole specifying rust memory and execution model task believe that this will be the case in the future (and it works now)".</p>
<p>Or is this just a bug and a PR to fix this would be merged?</p>



<a name="236756701"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/236756701" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#236756701">(Apr 29 2021 at 22:25)</a>:</h4>
<p>The purpose of the unsafe code guidelines was/is to prepare a detailed design for a future RFC, so it is to be expected that it is ahead of rust stabilization</p>



<a name="236760879"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/236760879" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#236760879">(Apr 29 2021 at 23:05)</a>:</h4>
<p>I wouldn't count on the "(and it works now)" for the slice/dst layout. I mean, it's true, but I actually dont think that part will be stabilized tbh</p>



<a name="236765605"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/236765605" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#236765605">(Apr 30 2021 at 00:00)</a>:</h4>
<p>Ok, thanks. I also have to apologize I entered the guidelines through a link onto the part wrt. DST-pointer layout and due to  that never properly read the intro. I just realized the intro says <code>"recommends" what kinds of things unsafe code can and cannot do</code> and so one. <span aria-label="face palm" class="emoji emoji-1f926" role="img" title="face palm">:face_palm:</span> . </p>
<p>I know it would be nice to be able to rely on people reading the intro, but people tend to skip intros so if someone happens to be very bored it might maybe be a nice idea to add some kind of "corner banner" to the books template. Something like "This document doesn't represent the current state of rust" with a slightly orange but non aggressive yellow background or similar.  I would say I will do so on my weekend, but I don't know if I have the time and I haven't done any non trivial CSS development since 5 years.</p>



<a name="236779348"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/236779348" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#236779348">(Apr 30 2021 at 03:12)</a>:</h4>
<p>slice layout is unlikely to be stabilized simply because there's already methods to get the raw parts and a function to make one from the raw parts. So unsafe code doesn't <em>need</em> a stable layout because you can call those stable methods and functions already.</p>



<a name="236783201"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/236783201" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#236783201">(Apr 30 2021 at 04:12)</a>:</h4>
<p>I guess. It would still be useful in the same way repr(C) is useful</p>



<a name="237188712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/237188712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#237188712">(May 03 2021 at 16:28)</a>:</h4>
<p>I don't think you're wrong, but sadly I also don't think I'm wrong.</p>



<a name="237205643"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/237205643" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#237205643">(May 03 2021 at 18:21)</a>:</h4>
<p>I don't think there's any thing happening that would stabilize the <em>layout</em> of a slice or fat pointer, just <a href="https://github.com/rust-lang/rfcs/pull/2580">https://github.com/rust-lang/rfcs/pull/2580</a> to make pulling them apart and putting them back together easier.</p>



<a name="237226242"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/237226242" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#237226242">(May 03 2021 at 20:46)</a>:</h4>
<p>Not having <code>&amp;[u8] is not FFI safe</code> warning when passing arguments into <code>extern "C"</code> functions would be nice</p>



<a name="237380816"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/237380816" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#237380816">(May 04 2021 at 19:02)</a>:</h4>
<p>&lt;<a href="https://docs.rs/chromium/0.0.2/chromium/struct.SharedSlice.html">https://docs.rs/chromium/0.0.2/chromium/struct.SharedSlice.html</a>&gt;</p>



<a name="237395618"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/237395618" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#237395618">(May 04 2021 at 20:50)</a>:</h4>
<p>talking about layout of these is not all that useful without also talking about how they participate in CCs.</p>



<a name="237395637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/237395637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#237395637">(May 04 2021 at 20:51)</a>:</h4>
<p>where they are quite special-cased.</p>



<a name="237406845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/237406845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#237406845">(May 04 2021 at 21:57)</a>:</h4>
<p>It would be great if a <code>buf: &amp;[T]</code> field were officially guaranteed to be layout compatible to two fields <code>ptr: *const T, size: usize</code>.</p>



<a name="237406996"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/237406996" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#237406996">(May 04 2021 at 21:58)</a>:</h4>
<p>It would give cbindgen opportunity to do this transformation automatically instead of offloading it to user like described in <a href="https://github.com/eqrion/cbindgen/issues/247#issuecomment-760571976">https://github.com/eqrion/cbindgen/issues/247#issuecomment-760571976</a>.</p>



<a name="237407317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/237407317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#237407317">(May 04 2021 at 22:00)</a>:</h4>
<p>This equivalence help to reuse a larger number of existing Rust structures across FFI instead of introducing some wrapper/adapter cruft.</p>



<a name="237413529"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/237413529" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#237413529">(May 04 2021 at 22:58)</a>:</h4>
<p>Reminds me a bit of the repr converstation for wasm about how it's splatted.  Could be interesting to have <code>repr(splatted)</code> defined for <code>&amp;[T]</code> to use it in <code>extern "C"</code> too.</p>



<a name="237697980"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/237697980" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#237697980">(May 06 2021 at 17:31)</a>:</h4>
<p><span class="user-mention silent" data-user-id="224471">Lokathor</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F/near/237380816">said</a>:</p>
<blockquote>
<p>&lt;<a href="https://docs.rs/chromium/0.0.2/chromium/struct.SharedSlice.html">https://docs.rs/chromium/0.0.2/chromium/struct.SharedSlice.html</a>&gt;</p>
</blockquote>
<p>Or the extra mile: <a href="https://getditto.github.io/safer_ffi/motivation/repr-c-forall.html">https://getditto.github.io/safer_ffi/motivation/repr-c-forall.html</a></p>



<a name="237702209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/237702209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#237702209">(May 06 2021 at 18:00)</a>:</h4>
<p>i mean the rest of the chromium crate does all this stuff too.</p>



<a name="237969311"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/237969311" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#237969311">(May 08 2021 at 16:48)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123856">Vadim Petrochenkov</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F/near/237406996">said</a>:</p>
<blockquote>
<p>It would give cbindgen opportunity to do this transformation automatically instead of offloading it to user like described in <a href="https://github.com/eqrion/cbindgen/issues/247#issuecomment-760571976">https://github.com/eqrion/cbindgen/issues/247#issuecomment-760571976</a>.</p>
</blockquote>
<p>why can't bindgen do it autoamtically now? it can generate wrapper functions that take slices, and that do the unpacking before calling the C function</p>



<a name="237982865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/237982865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#237982865">(May 08 2021 at 20:35)</a>:</h4>
<ul>
<li>bindgen makes Rust code that can call C code based on C header files.</li>
<li>cbindgen is the opposite: it makes C header files that let C code call Rust code.</li>
</ul>
<p>so bindgen could theoretically make extra Rust code to convert formats (though i wouldn't trust it to know when to do that automatically).</p>
<p>but cbindgen just makes C source so it can't add any Rust side code at all.</p>



<a name="238029125"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/238029125" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#238029125">(May 09 2021 at 11:21)</a>:</h4>
<p>Ah I see -- in principle cbindgen could have a macro on the Rust side that performs "C-ification" of the Rust library</p>



<a name="238029136"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/238029136" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#238029136">(May 09 2021 at 11:21)</a>:</h4>
<p>I assume this will be requires in many cases anyway (similar to exposing a C API from a C++ library)</p>



<a name="238043601"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/238043601" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#238043601">(May 09 2021 at 15:43)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F/near/238029125">said</a>:</p>
<blockquote>
<p>Ah I see -- in principle cbindgen could have a macro on the Rust side that performs "C-ification" of the Rust library</p>
</blockquote>
<p>That's exactly what <a href="https://docs.rs/safer-ffi">https://docs.rs/safer-ffi</a> does (<a href="https://getditto.github.io/safer_ffi/simple-examples/max.html">example</a>), although the current version does require that the caller explicitly use the repr C types; I guess a way to <strong>opt into</strong> auto-marshalling of Rust types into/from their ReprC counterparts could be added <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="238096746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did%20we%20stabilized%20the%20layout%20of%20%26%5BT%5D%2C%20%26dyn%20Trait%3F/near/238096746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Did.20we.20stabilized.20the.20layout.20of.20.26.5BT.5D.2C.20.26dyn.20Trait.3F.html#238096746">(May 10 2021 at 07:16)</a>:</h4>
<p>I think the intent for <code>cbindgen</code> is that it's a CLI program. You point the program at a rust crate and it generates C bindings for said crate. That way the crate maintainer doesn't have to think about doing any extra steps on their end.</p>
<p>having a system where proc-macros tag a bunch of stuff within the crate like <code>safer-ffi</code> does is a valid but totally alternate approach.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>